package com.suli.multimedia.controller;


import com.scenic.util.ResponseResult;
import com.suli.multimedia.entity.Image;
import com.suli.multimedia.entity.vo.ImageVO;
import com.suli.multimedia.service.ImageService;
import com.suli.multimedia.util.OssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author SuLi
 * @since 2022-03-23
 */
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/image")
public class ImageController {


    @Autowired
    ImageService imageService;
    @Autowired
    OssUtil ossUtil;

    /**
     * 获取走马图
     * @return 结果返回
     */
    @GetMapping("/getHorse")
    public ResponseResult getHorse(){
        Map<String, Object> image = imageService.getImage();
        return ResponseResult.ok().data(image);
    }

    /**
     * 插入图片
     * @param imageVO
     * @return
     */
    @PostMapping("/insertImage")
    public ResponseResult insertImage(@RequestBody ImageVO imageVO){

        if(imageService.insertImage(imageVO)){
            return ResponseResult.ok();
        }
        return ResponseResult.error();
    }

    /**
     * 根据景点id获取景点配图
     * @param scenicId 景点id
     * @return 景点配图
     */
    @GetMapping("/getScenicImage/{scenicId}")
    ResponseResult getImageByScenicId(@PathVariable Integer scenicId){
        Map<String, Object> image = imageService.getImage(scenicId);
        if(!ObjectUtils.isEmpty(image))
        return  ResponseResult.ok().data(image);
        return  ResponseResult.error().message("获取配图失败");
    }

    /**
     * 永久删除图片
     * @param imageId 图片id
     * @param imageUrl 图片id地址
     * @return 删除结果
     */
    @DeleteMapping("/deleteImage/{imageId}")
    public ResponseResult deleteImage(@PathVariable String imageId,
                                      @RequestParam String imageUrl){
        if(imageService.deleteImage(imageId)){
            imageUrl=imageUrl.substring(42);
            ossUtil.delete(imageUrl);
            return ResponseResult.ok().message("删除成功");
        }
        return ResponseResult.error().message("删除失败");
    }
    @PostMapping("/insertHorse")
    public ResponseResult insertHorse(@RequestBody Image image){
        if(imageService.insertHorse(image)){
            return ResponseResult.ok().message("添加成功");
        }
        return ResponseResult.error().message("添加失败");
    }
}

